﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DataLayer.Types;
using ViewModel.Finance;

namespace MyWpfApp.Finance
{
    /// <summary>
    /// Interaction logic for SavingsPlanListView.xaml
    /// </summary>
    public partial class SavingsPlanListView : UserControl
    {
        private SavingsPlansListViewModel viewModel;

        public SavingsPlanListView()
        {
            InitializeComponent();

            viewModel = DataContext as SavingsPlansListViewModel;
        }

        private void listView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ShowItemPropertiesDialog();
        }

        private void ItemPropertiesClicked(object sender, RoutedEventArgs e)
        {
            SavingsPlan item = listView.SelectedItem as SavingsPlan;
            ShowItemDialog(item);
        }

        private void NewItemClicked(object sender, RoutedEventArgs e)
        {
            SavingsPlan item = new SavingsPlan();
            item.StartDate = DateTime.Now;
            item.EndDate = DateTime.Now.AddDays(7);
            ShowItemDialog(item);
        }

        private void DeleteItem_Click(object sender, RoutedEventArgs e)
        {
            DeleteItem(listView.SelectedItem as SavingsPlan);
        }

        private void ShowItemPropertiesDialog()
        {
            SavingsPlan item = listView.SelectedItem as SavingsPlan;
            ShowItemDialog(item);
        }

        private void ShowItemDialog(SavingsPlan item)
        {
            SavingsPlanPropertiesView propertiesView = new SavingsPlanPropertiesView();
            propertiesView.DataContext = new SavingsPlanPropertiesViewModel(item);
            propertiesView.ShowDialog();
        }


        private void DeleteItem(SavingsPlan item)
        {
            if (MessageBox.Show("Are you sure you want to delete this item?\n\r\n\r" + item.Name) != MessageBoxResult.OK) return;

            viewModel.DeleteItem(item);
        }

    }
}
